package com.example.stock.util;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;

public class DialogUtils {
    private static ProgressDialog mProgressDialog;
    private static AlertDialog infoDialog;

    public static ProgressDialog showProgressDialog(Context ctx) {
        //必须每次建立新对象
        mProgressDialog = new ProgressDialog(ctx);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMessage("加载中...");
        if (mProgressDialog != null && !mProgressDialog.isShowing())
            mProgressDialog.show();
        return mProgressDialog;
    }

    public static void showProgressDialog(Context ctx, String msg) {
        mProgressDialog = new ProgressDialog(ctx);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(true);
        mProgressDialog.setMessage(msg);
        if (mProgressDialog != null && !mProgressDialog.isShowing())
            mProgressDialog.show();

    }

    public static void showProgressDialog(Context ctx, String msg, boolean cancelable) {
        mProgressDialog = new ProgressDialog(ctx);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(cancelable);
        mProgressDialog.setMessage(msg);
        if (mProgressDialog != null && !mProgressDialog.isShowing())
            mProgressDialog.show();
    }

    public static void showInfoDialog(Context ctx, String msg, DialogInterface.OnClickListener positive) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle("提示")    //标题
                .setCancelable(true).setMessage(msg).setPositiveButton("确定", positive);
        infoDialog = builder.create();
        if (!infoDialog.isShowing())
            infoDialog.show();
    }

    public static void showInfoDialog(Context ctx, String msg, DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative) {

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle("提示")    //标题
                .setCancelable(true).setMessage(msg).setPositiveButton("确定", positive).setNegativeButton("取消", negative);   //不响应back按钮
        infoDialog = builder.create();
        if (!infoDialog.isShowing())
            infoDialog.show();


    }

    public static void closeInfoDialog() {
        if (infoDialog != null && infoDialog.isShowing())
            infoDialog.dismiss();

    }

    public static void closeProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing())
            mProgressDialog.dismiss();

    }
}
